<?php
namespace App\Models;

class Permission extends \Spatie\Permission\Models\Permission
{
    protected $dispatchesEvents = [

    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'guard_name',
        'display_name',
        'is_menu',
        'icon',
        'parent_id',
        'sort'
    ];

    protected $hidden = [
        'guard_name',
        'created_at'
    ];

    public function setIconAttribute($value)
    {
        $this->attributes['icon'] = !empty($value) ? $value : '';
    }

    //子权限
    public function childs()
    {
        return $this->hasMany('App\Models\Permission','parent_id','id');
    }

    /**
     * @param $id
     * @return string
     */
    public function getPath($id)
    {
        static $arr = [];
        $current = $this->find($id);
        if (empty($current)) {
            return $id;
        }
        array_push($arr, $id);
        if ($current->parent_id == 0) {
            clock(count($arr) ? implode(',', array_reverse($arr)) : $id);
            return count($arr) ? implode(',', array_reverse($arr)) : $id;
        }
        clock($id);
        $this->getPath($current->parent_id, $arr);
    }

}
